<template>
  <div class="add_modle_dialog">
    <el-dialog title="型号信息设置" :visible.sync="visible" width="30%">
      <el-form :model="form" ref="form">
        <el-form-item label="型号名称" :label-width="formLabelWidth" prop="name" :rules="[rules,rulesLength50]">
          <el-input v-model="form.name" auto-complete="off" placeholder="请输入信息" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="厂商" :label-width="formLabelWidth" prop="supplier" :rules="[rules,rulesLength50]">
          <el-input v-model="form.supplier" auto-complete="off" placeholder="请输入信息" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="说明" :label-width="formLabelWidth" prop="content" :rules="[rulesLength200]">
          <el-input v-model="form.content" auto-complete="off" placeholder="请输入信息" size="mini"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="confirm('form')" v-if="!isedit">确 定</el-button>
        <el-button type="primary" @click="save('form')" v-if="isedit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import ApiClient from '@/utils/ApiClient'
  import ValidataRules from '@/utils/validateRules'


  export default {
    name: 'add_modle_dialog',
    props: {
      show: {type: Boolean, default: false},
      dataItem: {type: Object},
      isedit:{type:Boolean,default:false}
    },
    components: {},
    data() {
      return {
        formLabelWidth: '100px',
        form: {
          content: '',
          name: '',
          supplier: '',
        },
        rules: {required: true, message: '不能为空', trigger: 'blur'},
        rules1: {required: true, message: '不能为空', trigger: 'change'},
        rulesLength50: { validator: ValidataRules.limitMaxLength(50), trigger: 'blur' },
        rulesLength200: { validator: ValidataRules.limitMaxLength(200), trigger: 'blur' }
      }
    },
    methods: {
      close() {
        this.$emit('update:show', false)
      },
      //操作
      confirm(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            ApiClient.post('/backend/model/add.do', this.form).then(() => {
              this.close();
              this.$emit('success')
            })
          } else {
            return false;
          }
        });
      },
      save(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            ApiClient.put('/backend/model/update.do', this.form).then(() => {
              this.close();
              this.$emit('success')
            })
          } else {
            return false;
          }
        });
      }
    },
    computed: {
      visible: {
        get: function () {
          return this.show == false ? false : true
        },
        set: function () {
          this.$emit('update:show', false)
        }
      }
    },
    mounted() {
      if (this.dataItem) {
        this.form = _.cloneDeep(this.dataItem);
      }
    }
  }
</script>
<style lang="scss">
  .add_modle_dialog {
    .el-dialog__header {
      text-align: center;
    }
    .el-form-item {
      margin-bottom: 10px;
    }
    .el-form-item__content {
      text-align: left;
    }
  }
</style>

